package cn.com.ejiakanghu.service;

import cn.com.ejiakanghu.core.config.RedisKey;
import cn.com.ejiakanghu.core.enums.ResultCode;
import cn.com.ejiakanghu.core.utils.RespUtil;
import cn.com.ejiakanghu.core.vo.BaseResult;
import cn.com.ejiakanghu.form.AnnouncerQueryOrderForm;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.linlinjava.litemall.core.feie.FeiEService;
import org.linlinjava.litemall.core.feie.PrinterInfo;
import org.linlinjava.litemall.core.util.HttpUtil;
import org.linlinjava.litemall.db.domain.AgedmallAnnouncerBaseInfo;
import org.linlinjava.litemall.db.domain.AgedmallOrderMsg;
import org.linlinjava.litemall.db.domain.LitemallOrderGoods;
import org.linlinjava.litemall.db.service.AgedmallAnnouncerBaseInfoService;
import org.linlinjava.litemall.db.service.AgedmallOrderMsgService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AnnouncerManager {
    @Value("${litemall.announcer.orderNoticeUrl}")
    private String ORDERNOTICEURL;

    @Autowired
    private AgedmallAnnouncerBaseInfoService agedmallAnnouncerBaseInfoService;
    @Autowired
    private FeiEService feiEService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private AgedmallOrderMsgService agedmallOrderMsgService;

    public BaseResult addPrinter(String annoNum, Integer storeId, String sn, String key) {

        PrinterInfo printerInfo = new PrinterInfo();
        printerInfo.setSn(sn);
        printerInfo.setKey(key);
        printerInfo.setName(annoNum);
        List<PrinterInfo> printerInfos = new ArrayList<>();
        printerInfos.add(printerInfo);

        String result = feiEService.addPrinters(printerInfos);
        JSONObject object = JSONObject.parseObject(result);
        if (null != object && (int)object.get("ret") == 0) {
            JSONObject data = object.getJSONObject("data");
            if (null != data) {
                JSONArray jsonArray = (JSONArray)data.get("ok");
                if (jsonArray.size() > 0) {
                    JSONObject annoJson = new JSONObject();
                    annoJson.put("sn", sn);
                    annoJson.put("key", key);

                    AgedmallAnnouncerBaseInfo announcer = new AgedmallAnnouncerBaseInfo();
                    announcer.setAnnoNum(annoNum);
                    announcer.setAnnoType("feie");
                    announcer.setAnnoConf(annoJson.toJSONString());
                    announcer.setStoreId(storeId);
                    agedmallAnnouncerBaseInfoService.add(announcer);
                }
            }
        }

        return RespUtil.success(result);
    }

    public BaseResult queryOrder(AnnouncerQueryOrderForm announcerQueryOrderForm) {
        AgedmallAnnouncerBaseInfo announcerBaseInfo = agedmallAnnouncerBaseInfoService.queryByMac(announcerQueryOrderForm.getAnnoMac());
        if (null != announcerBaseInfo) {
            List<AgedmallOrderMsg> agedmallOrderMsgs = agedmallOrderMsgService.queryByStoreId(announcerBaseInfo.getStoreId());
            for (AgedmallOrderMsg agedmallOrderMsg : agedmallOrderMsgs) {
                redisService.lpush(RedisKey.ORDER_MSG, agedmallOrderMsg.getId()+"");
            }
        }
        return RespUtil.success();
    }


    public boolean orderNotice(Integer deviceId) {
        AgedmallAnnouncerBaseInfo announcerBaseInfo = agedmallAnnouncerBaseInfoService.queryById(deviceId);
        if (null == announcerBaseInfo) {
            return false;
        }
        Map<String, String> params = new HashMap<>();
        params.put("annoMac", announcerBaseInfo.getAnnoMac());
        params.put("type", "1");
        String result = HttpUtil.sendPost(ORDERNOTICEURL, params);
        BaseResult baseResult = JSONObject.parseObject(result, BaseResult.class);
        return ResultCode.SUCCESS.code() == baseResult.getCode();
    }
}
